<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>组织机构管理管理</title>
</head>
<jsp:include page="/common/meta.jsp"></jsp:include>
<jsp:include page="/common/resource.jsp"></jsp:include>
<!-- 公用方法-->
<script type="text/javascript" src="${base}/common/js/listPageMaster.js"></script>
<!-- 新增，编辑 ，删除 ，查看 js方法-->
<script type="text/javascript">
    /** 分页查询url地址*/
    // gridDataUrl = '${base}/hr/organization/OrganizationController/queryPaged';
    /** 初始化菜单树*/
    $(function () {
        initCompanyTree();
    });
    //新增动作
    function add() {
        var node = getCurrenNode();
        top.$.messager.confirm('确认','将要为《'+node.text+"》添加组织？",function(r){
            if (r){
                var attributes = node.attributes;

                /** 组织机构id*/
               var org_id =  attributes.org_id;
                /** 上级组织机构ID */
                var up_org_id =  attributes.up_org_id;
                /** 企业id */
                var company_id = attributes.company_id;

                var addUrl = "${base}/hr/organization/OrganizationController/goAddPage?org_id="+org_id+"&up_org_id="+up_org_id+"&company_id="+company_id;
                openEasyuiDialog("组织机构管理新增", addUrl, 500, 500);
            }
        });

    }

    //编辑动作
    function edit() {
        var row = $("#tblGrid").datagrid("getSelected");
        if (row == null) {
           top.$.messager.alert('提示', '请选择要编辑的数据', 'info');
            return;
        }
        var editUrl = "${base}/hr/organization/OrganizationController/edit/" + row.id;
        openEasyuiDialog("组织机构管理编辑", editUrl, 500, 500);
    }
    //查看动作
    function show() {
        var row = $("#tblGrid").datagrid("getSelected");
        if (row == null) {
           top.$.messager.alert('提示', '请选择要查看的数据', 'info');
            return;
        }
        //$("#save").remove();
        var showUrl = "${base}/hr/organization/OrganizationController/show/" + row.id;
        openEasyuiDialog("组织机构管理查看", showUrl, 500, 500);
    }


    //删除动作
    function deleteRow() {
        var deleteUrl = "${base}/hr/organization/OrganizationController/delete";
        var row = $("#tblGrid").datagrid("getSelected");
        if (!row) {
           top.$.messager.alert('提示', '请选择要删除的数据', 'info');
            return;
        }
       top.$.messager.confirm('提示', '确定要删除吗？', function (r) {
            if (r) {
                doDeleteRow(deleteUrl, {
                    "id": row.id
                });
            }
        });
    }
    /**  初始化组织机构树*/
    function  initCompanyTree() {
		$("#companyTree").tree({
            url: '${base}/hr/organization/OrganizationController/listQueryCompanyOrganizationTreeNode',
            method: 'GET',
            animate: true,
            lines: true,
            dnd: true,
            onBeforeDrag: function (node) {
                if (node.up_org_id == -1) {
                    return false;
                }
            },
            onDrop: function (target, source) {

                moveOrganization($(this).tree("getNode", target), source);
            },
            onClick: function (node) {
                if (node.state == 'open') {
                    $(this).tree('collapse', node.target)
                } else {
                    $(this).tree('expand', node.target)
                }
                loadList(node);
            },
            onLoadSuccess: function () {
                var tempNode = $(this).tree('getSelected');
                if (null == tempNode) {
                    $(this).tree("select", $(this).tree("getRoot").target);
                    tempNode = $(this).tree('getSelected');
                }
                $(this).tree('expand', tempNode.target);
                loadList(tempNode);
            },
            onBeforeDrop:function(target, source, point){
                var up_org_id =  source.attributes.up_org_id;

                /** 如果是负数，不允许拖动*/
                if(up_org_id <0){
                    return false;
				}
			},
            onBeforeExpand: function (node) {
                loadChildList(node);
            }
        });
    }
    /* 移动组织机构，修改上级所属**/
    function moveOrganization(target, source) {
        doAjaxRequest("${base}/hr/organization/OrganizationController/update", {
            "id": source.id,
            "up_org_id": target.id
        }, refreshTree);
    }

    /**
     * 加载子节点
     */
    loadChildList = function (node) {
        var attributes = node.attributes;

        /** 组织机构id*/
        var up_org_id =  node.id;
        /** 企业id */
        var company_id = attributes.company_id;
        $('#companyTree').tree('options').url = "${base}/hr/organization/OrganizationController/listCreateSubOrganizationTree?up_org_id="+up_org_id+"&company_id="+company_id;

    }
    /**
     * 加载右侧列表
     */
    loadList = function (node) {
        var up_idValue = '';
        if (node == null) {
            node = $('#companyTree').tree("getSelected");
            if (null == node) {
                node = $('#companyTree').tree("getRoot");
                up_idValue = node.id
            }
        }else {
            up_idValue = node.id;
        }

        var attributes = node.attributes;
        /** 上级组织机构ID */
        var up_org_id =  node.id;
        /** 企业id */
        var company_id = attributes.company_id;
	debugger;
        $('#tblGrid').datagrid({
            url: '${base}/hr/organization/OrganizationController/queryPaged',
            queryParams: {
                company_id: company_id,
                up_org_id: up_org_id
            }
        });
    } //查询
    function doSearch() {
       var org_name = $("#org_name").val();

        var up_org_id =  getCurrenNode().id;

       if(org_name != null && org_name !=""){
           up_org_id =null;
       }
        $('#tblGrid').datagrid({
            url: '${base}/hr/organization/OrganizationController/queryPaged',
            queryParams: {
                org_name: org_name,
                up_org_id: up_org_id
            }
        });
    }
    getCurrenNode = function () {
        var node = $('#companyTree').tree("getSelected");
        if (node == null) {
            node = $('#companyTree').tree("getRoot");
        }
        return node;
    }

    function updateTreeNode(data) {
        var node = $('#companyTree').tree('find', data.id);
        $("#companyTree").tree("update", {
            target: node.target,
            text: data.name
        });
    }

    /*** 节点树新增*/
    function addTreeNode(data) {
        /** 如果是一级模块。直接添加到跟节点上*/
        var  parent  = getCurrenNode().target;
        $("#companyTree").tree("append", {
            parent:parent,
            data: [{
                id: data.id,
                text: data.org_name,
                attributes: data,
                state: 'closed'
            }]
        });

        /** 刷新节树节点*/
        refreshTree();
    }
    /** 刷新树*/
    function refreshTree() {
        var   tempNode = getCurrenNode();
        loadList(tempNode);
    }
</script>
	<body>
	<div id="layout" class="easyui-layout" style="width: 100%; height: 100%; background: #fff;">
	    <!-- 查询条件 开始 -->
	    <jsp:include page="vo/conditionVO.jsp"></jsp:include>
		<!-- 组织机构树-->
		<jsp:include page="vo/companyTree.jsp"/>
	    <!-- 查询条件 结束 -->
	    <jsp:include page="page/listComponent.jsp"></jsp:include>
	</div>
	</body>
</html>
